package com.dongdongshop.controller;

import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;

import com.dongdongshop.data.PageBean;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.service.search.SearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 搜索服务
 * @Author Eayon
 * @Date 2020/4/3 18:41
 */
@RestController
@RequestMapping("search")
public class SearchController {

    private static final Logger log = LoggerFactory.getLogger(SearchController.class);

    @Autowired
    private SearchService searchService;



    /**
     * 按照商品名称从索引库中模糊查询
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{keyword}")
    public DataResult<PageBean> searchItemByKeyword(@PathVariable("keyword")String keyword
                                                  ,@RequestParam(value = "pageNum",required = true)Integer pageNum
                                                  ,@RequestParam(value = "pageSize",required = true)Integer pageSize){
        PageBean<TbItem> page = null;
        try {
            page = searchService.searchItemByKeyword(keyword,pageNum,pageSize);
        }catch (Exception e){
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult<PageBean>(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),page);
    }

}
